#include <QString>

#include "ChoiceRepDialog.h"
#include "ui_ChoiceRepDialog.h"


ChoiceRepDialog::ChoiceRepDialog(QWidget *parent, int save, const Image_t<double> &from) :
    QDialog(parent),
    ui(new Ui::ChoiceRepDialog)
{
    ui->setupUi(this);
    if(save){
        ui->spinBox_min->setRange(0,255);
        ui->spinBox_max->setRange(0,255);
        ui->spinBox_center->setRange(1,254);
    }
    ui->spinBox_center->setValue(128);

    int min,max,mean,dev;
    ui->tableView->model()->setData(ui->tableView->model()->index(0,1),QString("Red"));
    ui->tableView->model()->setData(ui->tableView->model()->index(0,2),QString("Green"));
    ui->tableView->model()->setData(ui->tableView->model()->index(0,3),QString("Blue"));
    ui->tableView->model()->setData(ui->tableView->model()->index(1,0),QString("Min"));
    ui->tableView->model()->setData(ui->tableView->model()->index(2,0),QString("Max"));
    ui->tableView->model()->setData(ui->tableView->model()->index(3,0),QString("Mean"));
    ui->tableView->model()->setData(ui->tableView->model()->index(4,0),QString("Std dev"));
    for(unsigned int c = 0; c < from.getNbChannels(); ++c) {
        min = from.min(c);
        max = from.max(c);
        mean = from.mean(c);
        dev = from.deviation(c);
        _mintotal = (min < _mintotal)? min : _mintotal;
        _maxtotal = (max > _maxtotal)? max : _maxtotal;
        ui->tableView->model()->setData(ui->tableView->model()->index(1,c+1),QString(min));
        ui->tableView->model()->setData(ui->tableView->model()->index(2,c+1),QString(max));
        ui->tableView->model()->setData(ui->tableView->model()->index(3,c+1),QString(mean));
        ui->tableView->model()->setData(ui->tableView->model()->index(4,c+1),QString(dev));
    }

    ui->spinBox_min->setValue(_mintotal);
    ui->spinBox_max->setValue(_maxtotal);
}

ChoiceRepDialog::~ChoiceRepDialog()
{
    delete ui;
}

void ChoiceRepDialog::changeParam(int j){
    switch(j){
        case 0: //Do nothing, we hide all the parameters and labels
        ui->spinBox_min->hide();ui->label_min->hide();
        ui->spinBox_max->hide();ui->label_max->hide();
        ui->spinBox_center->hide();ui->label_center->hide();
        break;

        case 1: //cut to interval is same than case 2

        case 2: //translation
        ui->spinBox_min->show();ui->label_min->show();
        ui->spinBox_max->show();ui->label_max->show();
        ui->spinBox_center->hide();ui->label_center->hide();
        break;

        case 3: //center on a value

        ui->spinBox_min->show();ui->label_min->show();
        ui->spinBox_max->show();ui->label_max->show();
        ui->spinBox_center->show();ui->label_center->show();
        break;
    }
}

double ChoiceRepDialog::getEqPixel(double value){
    double ret;
    static double factor = (_valuemax-_valuemin)/(_maxtotal-_mintotal);
    static double factorG = (_valuecenter-_valuemin)/(0-_mintotal);
    static double factorD = (_valuemax-_valuecenter)/(_maxtotal-0);
    if(_valuemode == 0){//do nothing

    }else if(_valuemode){//cut
        if(value > _valuemax) ret = _valuemax;
        if(value < _valuemin) ret = _valuemin;
    }else if(_valuemode == 2){//translation
        ret = factor * (value-_mintotal);
    }else if(_valuemode == 3){//centering
        if(value < 0){
            ret = factorG * (value-_mintotal);
        }else{
            ret = factorD * (value-0);
        }
    }
    return ret;
}
